﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using UCL;
using UCL.Forms;
using UCL.Data;

namespace DBKit5
{
    public partial class frmDbUpdate : Form
    {
        AppDataHelper adh;
        public IDatabaseHelper idh;
        string strLTSFDir;
        public frmDbUpdate()
        {
            InitializeComponent();
            ucConnectionBar1.ConnectionChangedHandler += ConnectionChangedEvent;
        }

        private void frmDbUpdate_Load(object sender, EventArgs e)
        {
            adh = new AppDataHelper();
            ucConnectionBar1.uc_init();

            strLTSFDir = string.Format("{0}\\ADFS\\LTSF", Application.StartupPath);
            if (!Directory.Exists(strLTSFDir)) Directory.CreateDirectory(strLTSFDir);
            FormLoad_LTSF();
        }
        private void ConnectionChangedEvent(IDatabaseHelper objidh)
        {
            idh = objidh;
        }

        // 加载LTSF文件
        private void FormLoad_LTSF()
        {
            cbbLTSF.Items.Clear();
            foreach (string fn in Directory.GetFiles(strLTSFDir))
            {
                cbbLTSF.Items.Add(Path.GetFileNameWithoutExtension(fn));
            }
        }

        private void btnPickFile_Click(object sender, EventArgs e)
        {
            this.ShowOpenFileDialog("模型文件(*.xml)|*.xml", fn => { tbPath.Text = fn; });
        }

        private void btnParse_Click(object sender, EventArgs e)
        {
            if (tbPath.TextLength == 0)
            {
                MessageBox.Show("请选择模型文件");
                return;
            }
            if (!System.IO.File.Exists(tbPath.Text))
            {
                MessageBox.Show("模型文件不存在,请重新选择一个模型文件");
                return;
            }
            if (idh == null)
            {
                MessageBox.Show("数据库连接无效");
                return;
            }
            tbSql.Text = idh.SO_GetUpdateTableStructureSqlsByXml(System.Xml.Linq.XElement.Load(tbPath.Text));
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            if (tbSql.TextLength == 0)
            {
                MessageBox.Show("请先执行[分析]操作");
                return;
            }
            idh.ExecuteNonQuery(tbSql.Text);
            MessageBox.Show("操作成功");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCreateSql_Click(object sender, EventArgs e)
        {
            if (tbPath.TextLength == 0)
            {
                MessageBox.Show("请选择模型文件");
                return;
            }
            if (!System.IO.File.Exists(tbPath.Text))
            {
                MessageBox.Show("模型文件不存在,请重新选择一个模型文件");
                return;
            }
            tbSql.Text = idh.SO_GetCreateTableStructureSqlsByXml(System.Xml.Linq.XElement.Load(tbPath.Text));
        }

        private void cbbLTSF_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbLTSF.SelectedItem == null) return;
            string path = string.Format("{0}\\{1}.xml", strLTSFDir, cbbLTSF.SelectedItem);
            if (!File.Exists(path))
            {
                MessageBox.Show("文件可能已删除");
                return;
            }
            tbPath.Text = path;
        }
    }
}
